<?php	namespace Common\Model;
use Hdphp\Model\Model;
class User extends Model
{
	//操作数据表
	protected $table = "user";
	//自动验证，必须通过create方法来触发
	protected $validate = array(
		//1.name名称;2.验证规则；3.提示信息;4.验证条件;5.验证时间
		array("username","required","请输入用户名",3,3),
		array("password","required","请输入密码",3,3),
	);
	/**
	 * 登陆
	 */
	/*public function login()
	{
			//自动验证
		if(!$this->create()) return false;
		//接收post数据
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$user = Db::select("select * from user where username='{$username}' and password='$password'");
		if($user){
			if($username != $user[0]['username']) return false;
			$uid = $user[0]['uid'];
			p($uid);
			$_SESSION['uid'] = $uid;
			p($_SESSION['uid']);
			header("location:index.php?m=Admin&c=Index");
		}else{
			return false;
		}
	}*/
	public function login()
	{
		if(!$this->create()) return false;
		$username = $_POST['username'];
		$password = $_POST['password'];
		$_SESSION['password']=$password;
		$data = $this -> where("username",$username) -> first();
		if($data['username'] != $username)
		{
			
			$this->error("用户名不存在");
			return false;
		}
		if($data['password'] != md5($password))
		{
			$this->error("密码错误");
			return false;
		}
		$_SESSION['uid'] = $data['uid'];
		$_SESSION['username'] = $username;
		
		return true;
	}
}